package com.qike.duola.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qike.duola.domain.*;
import com.qike.duola.enums.CarStatusEnums;
import com.qike.duola.enums.CarTransStatusEnums;
import com.qike.duola.enums.StatusEnums;
import com.qike.duola.enums.TransOrderStatusEnums;
import com.qike.duola.mapper.DuolaCarMapper;
import com.qike.duola.mapper.SysUserMapper;
import com.qike.duola.socket.NewCarLocationSocket;
import com.qike.duola.dto.CarLocationVo;
import com.qike.duola.dto.LocationEntity;
import com.qike.duola.service.DuolaTransOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Author: Eric.zhou
 * @Date: 2020/9/24 12:22
 */
@Component
//@Lazy
@RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueue
public class DirectReceiver {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private DuolaCarMapper carMapper ;

    @Autowired
    private NewCarLocationSocket carLocationSocket;
    @Autowired
    private DuolaTransOrderService transOrderService;
    @Autowired
    private SysUserMapper sysUserMapper;

    @RabbitHandler
    public void process(Map testMessage) {
        try {
//        System.out.println("DirectReceiver消费者收到消息  : " + testMessage.get("messageData").toString());
        LocationEntity locationEntity = JSONObject.parseObject(testMessage.get("messageData").toString(), LocationEntity.class);
        //redis 取出车辆信息
            DuolaCar car = null;
            if (redisTemplate.opsForValue().get(locationEntity.getTerminalPhone().substring(1))!=null){
                 car = JSONObject.parseObject(redisTemplate.opsForValue().get(locationEntity.getTerminalPhone().substring(1)).toString(),DuolaCar.class) ;
            }

        //发送长连接
        if (car==null) {
             car = carMapper.selectByDeviceId(locationEntity.getTerminalPhone().substring(1));
            if (car!=null){
                redisTemplate.opsForValue().set(locationEntity.getTerminalPhone().substring(1),JSON.toJSONString(car));
            }
        }
        if (car!=null){
            CarLocationVo vo = new CarLocationVo();
            vo.setCarNo(car.getCarNo());
            vo.setId(car.getId());
            vo.setCarType(car.getCarType());
            vo.setDirection(locationEntity.getDirection().toString());
            vo.setElevation(locationEntity.getElevation().toString());
            vo.setLatitude(locationEntity.getLatitude().doubleValue());
            vo.setLongitude(locationEntity.getLongitude().doubleValue());
            vo.setSpeed(locationEntity.getSpeed().toString());
            SysUserWithBLOBs sysUserWithBLOBs = sysUserMapper.selectByPrimaryKey(car.getDriverId());
            if (sysUserWithBLOBs!=null){
                vo.setName(sysUserWithBLOBs.getNickname());
            }
            vo.setModel(car.getCapacity());
            vo.setStatus(car.getTransStatus());
            if (car.getTransStatus()!=null){
                vo.setStatusStr(CarTransStatusEnums.statOf(car.getTransStatus()).getEmsg());
            }
            //判断车辆位置状态
            transOrderService.transStatus(vo);
            //获取车辆历史总量
            Double history = transOrderService.history(car.getId());
            vo.setHistory(history);
            carLocationSocket.sendMessage(JSON.toJSONString(vo));

            //更新车辆经纬度和速度
            car.setLatitude(locationEntity.getLatitude().toString());
            car.setLongitude(locationEntity.getLongitude().toString());
            car.setDirection(locationEntity.getDirection().toString());
            carMapper.updateByPrimaryKeySelective(car);
        }


            } catch (Exception e) {
            System.out.println(e);
            }
        }


    }

